package com.qiaoyanqing.ximalaya.discovermodule.discover_activity;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.AnchorDetailsAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.DiscoverAnchorDetailsService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.DiscoverAnchorDetailsSpecial;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.DiscoverAnchorDetailsVoice;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverAnchorDetails;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.SpecialTotle;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Voice;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.VoiceTotle;
import com.qiaoyanqing.ximalaya.url.Urlconfig;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class AnchorDetailsActivity extends AppCompatActivity implements Callback<DiscoverAnchorDetails> {

    private boolean isClick = true;
    private Retrofit retrofit;
    private String id;
    private String title;
    private TextView authorInfo;
    private ImageView authorPhotos;
    private CardView cv;
    private LinearLayout specialLayout;
    private ListView listView;
    private View headView;
    private TextView totle;
    private List<Voice> datas;
    private AnchorDetailsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anchor_details);
        initListView();
        initData();
    }

    private void initListView() {
        datas=new ArrayList<>();
        listView = ((ListView) findViewById(R.id.voice));

        headView=getLayoutInflater().inflate(R.layout.anchor_details_head,null);



        listView.addHeaderView(headView);
        adapter=new AnchorDetailsAdapter(datas,AnchorDetailsActivity.this);
        listView.setAdapter(adapter);
        specialLayout = ((LinearLayout) headView.findViewById(R.id.special));
        totle = ((TextView) headView.findViewById(R.id.totalCount));
        Intent intent = getIntent();
        id = intent.getStringExtra("uid");
        title = intent.getStringExtra("title");
    }

    private void initData() {


        //主播详情页个人信息（头部、照片、昵称和简介）
        retrofit = new Retrofit.Builder()
                .baseUrl(Urlconfig.DISCOVERANCHOR_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        DiscoverAnchorDetailsService service = retrofit.create(DiscoverAnchorDetailsService.class);
        service.getDetails(id).enqueue(this);


        //主播详情页个人信息（专辑）
        Retrofit specialRetrofit = new Retrofit.Builder()
                .baseUrl(Urlconfig.DISCOVERANCHORDETAILS_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        DiscoverAnchorDetailsSpecial specialService = specialRetrofit.create(DiscoverAnchorDetailsSpecial.class);
        specialService.getSpecial(id).enqueue(new Callback<SpecialTotle>() {
            @Override
            public void onResponse(Call<SpecialTotle> call, Response<SpecialTotle> response) {
                SpecialTotle data = response.body();
                if (data.getList().size() > 0) {
                    View view = getLayoutInflater().inflate(R.layout.special_item_title, null);
                    TextView total = (TextView) view.findViewById(R.id.totalCount);
                    total.setText(data.getTotalCount() + "");
                    specialLayout.addView(view);
                    for (int i = 0; i < data.getList().size(); i++) {
                        View specialView = getLayoutInflater().inflate(R.layout.special_item, null);
                        ImageView photo = (ImageView) specialView.findViewById(R.id.anchorPhoto);
                        TextView title = (TextView) specialView.findViewById(R.id.special_title);
                        TextView tracks = (TextView) specialView.findViewById(R.id.special_tracks);
                        Picasso.with(AnchorDetailsActivity.this).load(data.getList().get(i).getCoverMiddle()).into(photo);
                        title.setText(data.getList().get(i).getTitle());
                        tracks.setText(data.getList().get(i).getTracks());
                        specialLayout.addView(specialView);
                    }
                }




            }

            @Override
            public void onFailure(Call<SpecialTotle> call, Throwable t) {

            }
        });


        //主播详情页个人信息（声音）
        Retrofit voiceRetrofit = new Retrofit.Builder()
                .baseUrl(Urlconfig.DISCOVERANCHORVOICE_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        DiscoverAnchorDetailsVoice voiceService = voiceRetrofit.create(DiscoverAnchorDetailsVoice.class);
        voiceService.getVoice(id).enqueue(new Callback<VoiceTotle>() {
            @Override
            public void onResponse(Call<VoiceTotle> call, Response<VoiceTotle> response) {
                Log.d("TAG",response.body().toString());
                VoiceTotle data = response.body();
                totle.setText(data.getTotalCount()+"");
                datas.addAll(data.getList());
                adapter.notifyDataSetChanged();



            }

            @Override
            public void onFailure(Call<VoiceTotle> call, Throwable t) {

            }
        });



    }

    private void initView(String nickname, String photoUrl, String backUrl, String personalSignature) {


        //主播个人信息总内容
        ImageView headContainers = (ImageView) headView.findViewById(R.id.headContainer);

        //给布局设置背景
       // ImageView imageView = new ImageView(this);
      //  if(backUrl!=null){
            Picasso.with(this).load(backUrl).into(headContainers);
     //   }

       // Log.d("TAG", "主播详情页背景图片地址" + backUrl);
//        Drawable drawable = imageView.getDrawable();
//        headContainers.setBackground(drawable);


        //主播头像
        authorPhotos = (ImageView) headView.findViewById(R.id.authorPhoto);
        Picasso.with(this).load(photoUrl).into(authorPhotos);

        cv = ((CardView) headView.findViewById(R.id.photoContainer));
        //主播昵称
        final TextView authorName = (TextView) headView.findViewById(R.id.authorNam);
        authorName.setText(nickname);
        //主播简介
        authorInfo = (TextView) headView.findViewById(R.id.authorInfo);
        authorInfo.setText(personalSignature);
        //头像容器
        final CardView cardView = (CardView) headView.findViewById(R.id.photoContainer);

        Button backButton = ((Button)headView.findViewById(R.id.back_btn));
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //详情动画按钮
        Button detailsButton = ((Button) headView.findViewById(R.id.details));
        detailsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isClick) {
                    //按钮旋转
                    ObjectAnimator animatorBtn = ObjectAnimator.ofFloat(view, "rotation", 0f, 180f);
//                ObjectAnimator animator=ObjectAnimator.ofFloat(view,"rotation",0.0f,180f);
                    animatorBtn.setDuration(1000);
                    animatorBtn.start();

                    //头像的移动
                    ObjectAnimator animatorAuthorPhotos = ObjectAnimator.ofFloat(cv, "translationY", 0f, -80f);
                    animatorAuthorPhotos.setDuration(1000);
                    animatorAuthorPhotos.start();
                    //昵称的移动
                    ObjectAnimator animatorAuthorName = ObjectAnimator.ofFloat(authorName, "translationY", 0f, -60f);
                    animatorAuthorName.setDuration(1000);
                    animatorAuthorName.start();
                    //简介的移动
                    ObjectAnimator animatorAuthorInfo = ObjectAnimator.ofFloat(authorInfo, "translationY", 0f, -60f);
                    animatorAuthorInfo.setDuration(1000);
                    animatorAuthorInfo.start();
                    authorInfo.setSingleLine(false);


                    //主播头像消失
                    ObjectAnimator animatorAuthorAlpha = ObjectAnimator.ofFloat(authorPhotos, "alpha", 1f, 0f);
                    animatorAuthorAlpha.setDuration(1000);
                    animatorAuthorAlpha.start();
                    ObjectAnimator animatorCardView = ObjectAnimator.ofFloat(cardView, "alpha", 1f, 0f);
                    animatorCardView.setDuration(1000);
                    animatorCardView.start();


                    //图片旋转
                    ObjectAnimator animatorImage = ObjectAnimator.ofFloat(authorPhotos, "rotation", 0f, 180f);
//                ObjectAnimator animator=ObjectAnimator.ofFloat(view,"rotation",0.0f,180f);
                    animatorImage.setDuration(1000);
                    animatorImage.start();
                    isClick = false;
                } else {
                    //按钮旋转
                    ObjectAnimator animatorBtn = ObjectAnimator.ofFloat(view, "rotation", 180f, 0f);
//                ObjectAnimator animator=ObjectAnimator.ofFloat(view,"rotation",0.0f,180f);
                    animatorBtn.setDuration(1000);
                    animatorBtn.start();


                    //头像的移动
                    ObjectAnimator animatorAuthorPhotos = ObjectAnimator.ofFloat(cv, "translationY", -80f, 0f);
                    animatorAuthorPhotos.setDuration(1000);
                    animatorAuthorPhotos.start();
                    //昵称的移动
                    ObjectAnimator animatorAuthorName = ObjectAnimator.ofFloat(authorName, "translationY", -60f, 0f);
                    animatorAuthorName.setDuration(1000);
                    animatorAuthorName.start();
                    //简介的移动
                    ObjectAnimator animatorAuthorInfo = ObjectAnimator.ofFloat(authorInfo, "translationY", -80f, 0f);
                    animatorAuthorInfo.setDuration(1000);
                    animatorAuthorInfo.start();
                    authorInfo.setSingleLine(true);

                    //主播头像消失
                    ObjectAnimator animatorAuthorAlpha = ObjectAnimator.ofFloat(authorPhotos, "alpha", 0f, 1f);
                    animatorAuthorAlpha.setDuration(1000);
                    animatorAuthorAlpha.start();
                    ObjectAnimator animatorCardView = ObjectAnimator.ofFloat(cardView, "alpha", 0f, 1f);
                    animatorCardView.setDuration(1000);
                    animatorCardView.start();


                    //图片旋转
                    ObjectAnimator animatorImage = ObjectAnimator.ofFloat(authorPhotos, "rotation", 180f, 0f);
//                ObjectAnimator animator=ObjectAnimator.ofFloat(view,"rotation",0.0f,180f);
                    animatorImage.setDuration(1000);
                    animatorImage.start();
                    isClick = true;
                }
            }
        });

        //

    }

    @Override
    public void onResponse(Call<DiscoverAnchorDetails> call, Response<DiscoverAnchorDetails> response) {
        //昵称
        String nickname = response.body().getNickname();
        String photoUrl = response.body().getMobileMiddleLogo();
        String backUrl = response.body().getBackgroundLogo();
        String personalSignature = response.body().getPersonalSignature();
        initView(nickname, photoUrl, backUrl, personalSignature);


    }

    @Override
    public void onFailure(Call<DiscoverAnchorDetails> call, Throwable t) {

    }
}
